package org.example.user.service.impl;

import org.example.user.service.MockSmsCodeService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MockSmsCodeServiceImpl implements MockSmsCodeService {
    // 内存存储验证码
    private final Map<String, String> codeStore = new ConcurrentHashMap<>();

    // 模拟发送验证码
    public String sendCode(String phone) {
        String code = String.format("%06d", new Random().nextInt(999999));
        codeStore.put(phone, code);
        log.info("[模拟短信] 手机号 {} 的验证码: {}", phone, code);
        return code;
    }

    // 验证码校验
    public boolean verifyCode(String phone, String code) {
        return code != null && code.equals(codeStore.get(phone));
    }

    // 清除验证码
    public void clearCode(String phone) {
        codeStore.remove(phone);
    }
}